package Game;
import java.io.Serializable;


/**
 * Cart.java
 *
 * This class represents a new Cart object in the Oregon Trail game. A Cart is one of the Vehicle types 
 * in the Team Mexican Buzz implementation of Oregon Trail
 * A cart is composed of a yoke, an axel, and two wheels and is pulled by a Donkey object
 * 
 * @author Raymond DeCuir, rdecuir3 at gatech.edu
 * @version 1.4
 *
 */
public class Cart extends Vehicle implements Serializable {

	// The different parts of the cart, there is only ever one instance of these variables, as in whenever a cart is repaired, rather than replacing the
	// VehiclePart with a new object, the part is instead only set to functional 
	private VehicleNode yoke, axle, rightWheel, leftWheel;
	private Donkey donkey;
	
	
	/**
	 * Constructor for the Cart object, this constructor creates the yoke, axel, right and left wheel, and donkey objects
	 * 
	 */
	public Cart()
	{
		parts = new VehicleNode[5];
		this.weightCapacity = GameDriver.getItemList().getItemWeight( GameDriver.getItemList().getIdOf("name", "Cart") );
		yoke = new VehicleNode( GameDriver.getItemList().getIdOf("name", "Cart Yoke") );
		parts[0] = yoke;
		axle = new VehicleNode( GameDriver.getItemList().getIdOf("name", "Cart Axle") );
		parts[1] = axle;
		leftWheel = new VehicleNode( GameDriver.getItemList().getIdOf("name", "Cart Wheel") );
		parts[2] = leftWheel;
		rightWheel = new VehicleNode( GameDriver.getItemList().getIdOf("name", "Cart Wheel") );
		parts[3] = rightWheel;
		donkey = new Donkey();
	}
	
	
	/**
	 * isFunctional
	 * 
	 * This method returns a boolean denoting if a vehicle is functional to travel or not
	 * 
	 * @return A boolean denoting if a vehicle can travel or not
	 */
	
	public boolean isFunctional() {
		return findBrokenPart() == -1 && donkey.isAlive();
	}
	
	
	/**
	 * findBrokenPart()
	 * 
	 * This method traverses all of the parts of the Cart and checks to make sure that all of them are functional, 
	 * if one is found to be broken, that part is returned. Otherwise the method returns null
	 * 
	 * @return The id of the broken part, or -1 if no parts are broken 
	 */
	public int findBrokenPart()
	{
		for ( VehicleNode part : parts)
		{
			if ( !part.functional )
			{
				return part.id;
			}
		}
		if (donkey.isAlive())
		{
			return GameDriver.getItemList().getIdOf("name", "Donkey");
		}
		return -1;
	}
	
	
	/**
	 * breakRandomVehiclePart
	 * 
	 * This method randomly breaks a part belonging to that vehicle
	 */
	public void breakRandomVehiclePart()
	{
		if ( Math.random() < .85)
			parts[(int)Math.random() * parts.length ].functional = false;
		else 
			donkey.setHealthStatus(HealthState.DEAD);
	}
	

	/**
	 * repair
	 * 
	 * Sets all parts of a vehicle to functional again so that it may move
	 * 
	 */

	public void repair() 
	{
		for ( VehicleNode part : parts)
		{
			part.functional = true;
		}
		donkey.setHealthStatus(HealthState.NORMAL);
	}
	
	/**
	 * Gets the donkey object for the cart class
	 * 
	 * @return The instance of donkey in the cart
	 */
	public Donkey getDonkey()
	{
		return donkey;
	}
	
	/**
	 * Sets the new Donkey for the cart object
	 * 
	 * @param donkey The new Donkey
	 */
	public void setDonkey( Donkey donkey)
	{
		this.donkey = donkey;
	}

	

}
